package com.mage;

import freemarker.template.Configuration;
import freemarker.template.Template;

import java.io.File;
import java.io.FileWriter;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 *	设置
 */
public class FreemarkerApp12 {
	private static final String TEMPLATE_NAME = "settings";
	private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");


	public static void main(String[] args) throws Exception {
		Configuration configuration = new Configuration(Configuration.VERSION_2_3_28);
		configuration.setDirectoryForTemplateLoading(new File("src/main/templates"));
		configuration.setDefaultEncoding("utf-8");

		// 设置，属性都是字符串
		Properties props = new Properties();
		props.put("classic_compatible", "true"); // 设置值为null时不报空指针错
		props.put("number_format", "#.##");  // 设置精确的小数点位数
		props.put("boolean_format", "yes,no"); // 设置布尔值输出的值
		props.put("default_encoding", "utf8"); // 会覆盖之前设置
		props.put("date_format", "yyyy年MM月dd日"); // 设置日期格式
		props.put("time_format", "HH点mm分"); // 设置时间格式
		props.put("datetime_format", "yyyy年MM月dd日 HH点mm分"); // 设置日期时间格式
		props.put("auto_import", "'lib/mylib.ftl' as mylib"); // 设置全局的导入Freemarker类库
		configuration.setSettings(props);

		Template template = configuration.getTemplate(String.format("%s.ftl", TEMPLATE_NAME));
		Map<String, Object> dataModel = new HashMap<>();

		dataModel.put("name", null); // 字符串
		dataModel.put("wight", 160.8f); // 浮点型
		dataModel.put("money", 10977887989.8756d); // 双精度
		dataModel.put("isMale", true);
		dataModel.put("birthday", simpleDateFormat.parse("1987-10-10 12:13:12"));

		FileWriter out =  new FileWriter(new File(String.format("src/main/out/%s.html", TEMPLATE_NAME)));
		template.process(dataModel, out);
		out.close();
	}
}
